// pages/businessReg/bankReg.js
import {
  MerchantRegister,
  getProvinces,
  uploadMerchant,
  bankList,
  bankBranchInfo
} from '../../api.js'
let app = getApp();
Page({
  data: {
    showProvince: false,
    showCity: false,
    index: -1,
    index2: -1,
    provinces: [], //省
    cities: [], //市
    Images: {
      // SETTLE_BANKCARD: '', //结算银行卡
      logo_url: '', //手持银行卡
    },
    cardNo:'',
    toAuth: {
      cardNo: '', //银行账户
      bankProvince: '', //开户省
      bankCity: '', //开户市
      headBankCode: '', //开户总行编码
      bankCode: '', //支行编码
      openMini: 0, //是否开通小程序  //0 开通  1 不开通
      fileInfo: { // 照片信息
        // SETTLE_BANKCARD: {}, //银行卡
        logo_url: {}, //店铺LOGO
      }
    },
    headBank:[],
    headBank_index:-1,
    bankCode:[],
    bankCode_index:-1
  },
  onLoad: function(options) {
    var jsonStr = options.toAuth;
    var that = this
    jsonStr = jsonStr.replace(" ", "");
    if (typeof jsonStr != 'object') {
      jsonStr = jsonStr.replace(/\ufeff/g, "");
      var jj = JSON.parse(jsonStr);
      that.setData({
        toAuth:jj
      })
    }
    var that = this
    getProvinces({
      token: app.globalData.token
    }).then(res => {
      let provinces = res.data.data
      this.setData({
        provinces
      })   
    })
    bankList({
      token: app.globalData.token
    }).then(res =>{
      this.setData({
        headBank:res.data.data
      })
    })
  },
  onShow: function() {

  },
  chooseProvince(e) {
    let label = e.currentTarget.dataset.address;
    this.setData({
      [label]: true
    })
  },
  bindProvince: function(e) {
    this.setData({
      index: e.detail.value,
      showCity:false,
      index2:-1,
      headBank_index:-1,
      bankCode:'',
      bankCode_index:-1
    })
    if (this.data.cities.length > 0) {
      this.setData({
        cities: [],
        showCity: false
      })
    }
    let cities = this.data.provinces[this.data.index].children;
    let bankProvince = this.data.provinces[this.data.index].value;
    this.setData({
      cities,
      'toAuth.bankProvince': bankProvince
    })
  },
  bindCity: function(e) {
    this.setData({
      index2: e.detail.value,
      headBank_index: -1,
      bankCode: '',
      bankCode_index:-1
    })
    let bankCity = this.data.cities[this.data.index2].value
    this.setData({
      'toAuth.bankCity': bankCity
    })
    
  },
  bindHeadBank:function(e){    
    if (this.data.index == -1 || this.data.index2 == -1){
      wx.showToast({
        title: '请先选择开户省市',
        icon:'none'
      })
      return false;
    }
    wx.showLoading({
      title: '正在查询支行',
    })
    var that = this
    var textnum = that.data.headBank[e.detail.value].name + '股份有限公司'    
    that.data.toAuth.headBankCode = that.data.headBank[e.detail.value].code
    that.setData({
      headBank_index: e.detail.value
    })
    bankBranchInfo({
      token: app.globalData.token,
      headBankCode: that.data.toAuth.headBankCode,
      provinceCode: that.data.toAuth.bankProvince,
      cityCode: that.data.toAuth.bankCity
    }).then(res =>{
      wx.hideLoading()
      if (res.data.data == '') {
        that.setData({
          branchBank: [{ id: '', name: '暂无数据' }],
          branchBank_index: 0
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      }else{
        for (var i = 0; i < res.data.data.length; i++) {
          res.data.data[i].name = res.data.data[i].name.replace(textnum, '')
        }
        that.setData({
          bankCode:res.data.data
        })
      }
    })
  },
  bindBank:function(e){
    var that = this;
    that.data.toAuth.bankCode = that.data.bankCode[e.detail.value].id
    that.setData({
      bankCode_index: e.detail.value
    })
  },
  openMini:function(e){
    var num = e.detail.value;
    this.data.toAuth.openMini = e.detail.value
  },
  formSubmit: function (e) {
    var cardNo = e.detail.value.cardNo
    var that = this;
    var _form = that.data.toAuth;
    _form.cardNo = cardNo
    console.log(_form.cardNo)
    if (!_form.cardNo) {
      wx.showToast({
        title: '请填写银行账号',
        icon: 'none'
      })
      return false;
    } else if (!this.data.toAuth.bankProvince || !this.data.toAuth.bankCity){
      wx.showToast({
        title: '请选择开户省市',
        icon: 'none'
      })
      return false;
    }
    console.log(that.data.toAuth)
    MerchantRegister({
      token: app.globalData.token,
      merFullName: _form.merFullName,
      merShortName: _form.merShortName,
      merCertType: _form.merCertType,
      merCertNo: _form.merCertNo,
      legalIdCard: _form.legalIdCard,
      legalName: _form.legalName,
      merAddress: _form.merAddress,
      merCity: _form.merCity,
      merDistrict: _form.merDistrict,
      merProvince: _form.merProvince,
      merContactName: _form.merContactName,
      merContactPhone: _form.merContactPhone,
      // merContactEmail: _form.merContactEmail,
      taxRegistCert: _form.taxRegistCert,
      accountLicense: _form.accountLicense,
      orgCode: _form.orgCode,
      isOrgCodeLong: _form.isOrgCodeLong,
      orgCodeExpiry: _form.orgCodeExpiry,
      bankCity: _form.bankCity,
      bankCode: _form.bankCode,
      bankProvince: _form.bankProvince,
      cardNo: _form.cardNo,
      headBankCode: _form.headBankCode,
      openMini: _form.openMini,
      fileInfo: _form.fileInfo,
      CORP_CODE: _form.CORP_CODE,
      TAX_CODE: _form.TAX_CODE,
      ORG_CODE: _form.ORG_CODE
    }).then(res =>{
      console.log(res.data.code)
      if(res.data.code == 666){
        wx.showToast({
          title: '提交成功',
        })
        setTimeout(function(){
          wx.navigateTo({
            url: '/pages/is_auth/is_auth?is_auth=1',
          })
        },1500)
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  logo_url: function (obj) {
    var that = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        console.log(res)
        wx.showLoading({
          title: '正在加载图片',
          icon: 'loading'
        })
        var url = res.tempFilePaths;
        wx.uploadFile({
          url: 'https://tool.growthupup.com/api/public/index/upload/uploadMerchant',
          filePath: url[0],
          name: 'imgfile',
          formData: {
            'token': app.globalData.token
          },
          success: function (res) {
            var jsonStr = res.data;
            jsonStr = jsonStr.replace(" ", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, "");
              var jj = JSON.parse(jsonStr);
              res.data = jj;
            }
            if (res.data.code == 666) {
              wx.hideLoading();
              that.data.toAuth.fileInfo.logo_url.url = res.data.data.url
              that.data.toAuth.fileInfo.logo_url.thumb = res.data.data.thumb
              that.data.toAuth.fileInfo.logo_url._file = res.data.data.base_image
              that.data.Images.logo_url = res.data.data.url
              that.setData({
                Images: that.data.Images
              })
            } else if (res.data.code == 111111) {
              wx.showToast({
                title: '登录失效，请重新登录',
                icon: 'none',
              })
              setTimeout(function () {
                wx.reLaunch({
                  url: '/pages/user/login/login',
                })
              }, 1500)
            }
          }
        })
      },
    })
  },
})